package com.boc.ewr.cornJob;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boc.ewr.bcimp.entity.Pro;
import com.boc.ewr.bcimp.service.IProService;
import com.boc.ewr.business.entity.ProPrbsdList;
import com.boc.ewr.business.service.IProPrbsdListService;
import com.boc.ewr.risk.entity.CaUnpriaccUnusual;
import com.boc.ewr.util.MsgCategoryEnum;
import com.boc.ewr.util.ToUserUtil;
import com.boc.ewr.util.file.enumeration.ProSendStatusEnum;
import com.boc.ewr.util.msg.MessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.ComboModel;
import org.jeecg.common.util.DateUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @author : Wzz
 * @create : 2023-04- 20- 15:30
 * @desc :问题提示-已签约账户地址为空-推送至对账人员
 */
@Slf4j
public class SgdEptAddrAcctJob implements Job {
    @Autowired
    private IProService proService;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Autowired
    private IProPrbsdListService proPrbsdListService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info(" Job Execution key：" + jobExecutionContext.getJobDetail().getKey());
        log.info(String.format(" 已签约账户地址为空账户推送 SgdEptAddrAcctJob 时间:" + DateUtils.getTimestamp()));

        // 清除之前数据
        proPrbsdListService.remove(
                new LambdaQueryWrapper<ProPrbsdList>()
                        .eq(ProPrbsdList::getSendSta, ProSendStatusEnum.EMTADDR.getIndex())
        );

        /*
         * 取PRO文件中第一个联系人不为空的账户为已签约账户，地址为空的是： pro里面邮寄地址是空的就是。
         * */
        List<Pro> signedAddrEpt = proService.getSignedAddrEpt();

        // 结果保存至问题提示-协议清单
        List<ProPrbsdList> sendList = new ArrayList<>();
        for (Pro pro : signedAddrEpt) {
            ProPrbsdList proPrbsdList = new ProPrbsdList();
            BeanUtils.copyProperties(pro, proPrbsdList);
            proPrbsdList.setSendSta(ProSendStatusEnum.EMTADDR.getIndex());
            sendList.add(proPrbsdList);
        }
        proPrbsdListService.saveOrUpdateBatch(sendList);
        // 消息推送
        this.sendMsg(sendList);
    }

    private void sendMsg(Collection<ProPrbsdList> entityList) {
        // 获取推送用户
        Set<String> branchNoSet = new HashSet();
        for (ProPrbsdList proPrbsdList : entityList) {
            branchNoSet.add(proPrbsdList.getSignedOrg());
        }
        String toUser = ToUserUtil.genToUser(branchNoSet);
        // String toUser = "admin";
        if (toUser != null) {
            MessageDTO messageDTO = new MessageDTO(
                    "system",
                    toUser,
                    "问题提示-已签约账户地址为空",
                    "协议清单中存在已签约账户地址为空账户，请及时查看并确认是否存在问题",
                    MsgCategoryEnum.TEMP_WORK.getIndex()
            );
            sysBaseAPI.sendSysAnnouncement(messageDTO);
        }
    }
}
